package rentadomus

import org.springframework.dao.DataIntegrityViolationException
class OfficeController {

    //def index() { }
    def scaffold = Office
    def save() {
        def officeInstance = new Office(params)
        def placeInstance = new Place(params)
        placeInstance.idPerson = session.person
        officeInstance.idPlace = placeInstance
        if (placeInstance.save(flush: true)) {
            if (!officeInstance.save(flush: true)) {
                render(view: "create", model: [localInstance: localInstance])
                return
            }
        }
        redirect(controller:"uploadImage", action: "main", id: placeInstance.id)
    }
    
  def delete(Long id) {
        def warehouseInstance = Office.get(id)
        
        if (!warehouseInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'warehouse.label', default: 'Warehouse'), id])
            redirect(action: "list")
            return
        }
        def placeInstance = Place.get(warehouseInstance.idPlace.id)
        try {
            warehouseInstance.delete(flush: true)
            placeInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'warehouse.label', default: 'Warehouse'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'warehouse.label', default: 'Warehouse'), id])
            redirect(action: "show", id: id)
        }
    }    
     def filterSearch(Integer max){
        def option = params.option
        if(option=="1"){
            def filter = params.filter
            def fVal=params.value
            def tVal= params.value2
            params.max = Math.min(max ?: 10, 100)
            [officeInstanceList: Office.list(params), officeInstanceTotal: Office.count(), valueP: fVal, 
                valueP2: tVal, placeInstanceList:Place.list(params), filterB: filter]
        }
        else{
            def value = params.value
            def filter = params.filter

            params.max = Math.min(max ?: 10, 100)
            [officeInstanceList: Office.list(params), officeInstanceTotal: Office.count(), valueP: value, 
                placeInstanceList:Place.list(params), filterB: filter]
        }
    }
    
    def show(Long id) {
        def officeInstance = Office.get(id)
        if (!officeInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'warehouse.label', default: 'Warehouse'), id])
            redirect(action: "list")
            return
        }
        def placeInstance = Place.get(officeInstance.idPlace.id)
        def visible=session.person && placeInstance.getIdPerson().getId()==session.person.id
        [officeInstance: officeInstance, placeInstance: placeInstance,phones:Phone.findAllByIdPerson(placeInstance.getIdPerson()),visible:visible]
    }
}

